<?xml version="1.0" encoding="UTF-8"?>
<!--
CheckStyle XML Style Sheet by Quinn Taylor <quinntaylor@mac.com>
Adapted from an original by Stephane Bailliez <sbailliez@apache.org>
For XSL transformation of XML reports <http://checkstyle.sourceforge.net>
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="html" indent="yes"/>
	<xsl:decimal-format decimal-separator="." grouping-separator=","/>
	<xsl:template match="checkstyle">
		<html>
			<head>
				<style type="text/css" media="all">
		body {
			margin-left: 10;
			margin-right: 10;
			font: normal 80% Helvetica, Arial, sans-serif;
			background-color: #FFFFFF;
			color: #000000;
		}
		h1, h2 {
			margin-top: 0;
			margin-bottom: 5px;
		}
		h1 {
			font-size: 160%;
		}
		h2 {
			font-size:120%;
		}
		h3 {
			font-size:100%;
			background: #525D76;
			color: white;
			text-decoration: none;
			padding: 3px;
			margin: 10px 0 0;
			border: solid 1px black;
			border-bottom: none;
		}
		table {
			width: 100%;
			border-width: 0;
			border-collapse: collapse;
		}
		table, th, td {
			font-size:100%;
			border: none
		}
		th, td {
			vertical-align: top;
			padding: 1px 4px;
		}
		th {
			text-align: left;
			background: #ccc;
			color: black;
			white-space: nowrap;
			width: 1;
		}
		td.w {
			width: 100%;
		}
		.a td { 
			background: #fff;
		}
		.b td { 
			background: #eee;
		}
		a:link {
			color: #003C96;
			background-color: inherit;
		}
		a:visited {
			color: #47a;
			background-color: inherit;
		}
		a:hover {
			text-decoration: none;
		}
		#summary {
			width: auto;
		}
		#byErrors, #byAlpha {
			padding: 0;
		}
		#byErrors {
			padding-right: 5px;
		}
		#byAlpha {
			padding-left: 5px;
		}
		table {
			border: solid 1px black;
		}
		#index {
			border: none;
		}
				</style>
				<style type="text/css" media="print">
		a:link, a:visited, a:hover {
			text-decoration: none;
			color: black;
		}
		h3 {
			color: black;
			background-color: white;
			padding-bottom: 2px;
			border: none;
			padding: 0 0 2px;
		}
		th {
			border-bottom: solid 1px black;
		}
		#summary th {
			border: none;
		}
		#byErrors, th.c, td.c {
			display: none;
		}
		#byAlpha {
			padding: 0;
		}
				</style>
		<xsl:apply-templates select="project" mode="title"/>
			</head>
			<body id="top">
				<xsl:choose>
					<!-- If there are no error, print header and a green banner. -->
					<xsl:when test="count(file/error) = 0">
						<center>
							<xsl:apply-templates select="project" mode="header"/>
							<h2><a href="http://checkstyle.sourceforge.net/">CheckStyle</a> Report
							<xsl:if test="checkdate != ''"> &#8212; <xsl:value-of select="checkdate"/></xsl:if></h2>
							<h3 style="background-color: #55b05a; text-align: center; font-size: 130%; font-weight: normal;">Congratulations, no style errors found<xsl:if test="filecount != ''"> in <xsl:value-of select="filecount"/> files</xsl:if>!</h3>
						</center>
					</xsl:when>
					<!-- Otherwise, print error summary & detail sections. -->
					<xsl:otherwise>
						<div style="float: left; white-space: nowrap;">
							<xsl:apply-templates select="project" mode="header"/>
							<h2><a href="http://checkstyle.sourceforge.net/">CheckStyle</a> Report
							<xsl:if test="checkdate != ''"> &#8212; <xsl:value-of select="checkdate"/></xsl:if></h2>
						</div>
						<div style="float: right; white-space: nowrap;">
							<xsl:apply-templates select="." mode="summary"/>
						</div>
						<div style="clear: both;" />
						<!-- Listing of files with errors, by # of errors, then by name. -->
						<table id="index">
							<tr>
								<td id="byErrors">
									<xsl:apply-templates select="." mode="filelist-errors"/>
								</td>
								<td id="byAlpha">
									<xsl:apply-templates select="." mode="filelist-alpha"/>
								</td>
							</tr>
						</table>
						<!-- For each file with errors, print the errors found. -->
						<xsl:for-each select="file">
							<xsl:sort select="@name"/>
							<xsl:apply-templates select="."/>
						</xsl:for-each>
					</xsl:otherwise>
				</xsl:choose>
			</body>
		</html>
	</xsl:template>
	
	<!-- Create HTML page title  -->
	<xsl:template match="project" mode="title">
		<title>CheckStyle Report: <xsl:value-of select="@name"/></title>
	</xsl:template>

	<!-- Header with project name and revision, if present.  -->
	<xsl:template match="project" mode="header">
		<h1><a href="{@href}"><xsl:value-of select="@name"/></a>
			<xsl:if test="@revision != ''">&#160;&#8212; Revision <xsl:value-of select="@revision"/></xsl:if>
		</h1>
	</xsl:template>
	
	<!-- Summary statistics -->
	<xsl:template match="checkstyle" mode="summary">
		<xsl:variable name="totalFiles" select="filecount"/>
		<xsl:variable name="errorFiles" select="count(file)"/>
		<xsl:variable name="errorCount" select="count(file/error)"/>
		<table id="summary">
			<tr>
				<th>Total Errors</th>
				<td><xsl:value-of select="$errorCount"/></td>
				<td colspan="2"></td>
			</tr>
			<tr>
				<th>Files Examined</th>
				<td><xsl:value-of select="$totalFiles"/></td>
				<td>&#160;Errors per file:</td>
				<td align="right"><xsl:value-of select="format-number($errorCount div $totalFiles, '#.00')"/></td>
			</tr>
			<tr>
				<th>Files with Errors</th>
				<td><xsl:value-of select="$errorFiles"/> (<xsl:value-of select="format-number($errorFiles div $totalFiles, '#%')"/>)</td>
				<td>&#160;Errors per file:</td>
				<td align="right"><xsl:value-of select="format-number($errorCount div $errorFiles, '#.00')"/></td>
			</tr>
		</table>
	</xsl:template>
	<!-- Print a list of files with errors, sorted alphabetically. -->
	<xsl:template match="checkstyle" mode="filelist-alpha">
		<h3>Files (Alphabetical)</h3>
		<table>
			<tr>
				<th>Filename</th>
				<th>Errors</th>
			</tr>
			<xsl:for-each select="file">
				<xsl:sort select="@name"/>
				<tr>
					<xsl:call-template name="alternated-row"/>
					<td>
						<a href="#f-{@name}">
							<xsl:value-of select="@name"/>
						</a>
					</td>
					<td>
						<xsl:value-of select="count(error)"/>
					</td>
				</tr>
			</xsl:for-each>
		</table>
	</xsl:template>
	<!-- Print a list of files with errors, sorted by number of errors. -->
	<xsl:template match="checkstyle" mode="filelist-errors">
		<h3>Files (By Most Errors)</h3>
		<table>
			<tr>
				<th>Errors</th>
				<th>Filename</th>
			</tr>
			<xsl:for-each select="file">
				<xsl:sort data-type="number" order="descending" select="count(error)"/>
				<tr>
					<xsl:call-template name="alternated-row"/>
					<td>
						<xsl:value-of select="count(error)"/>
					</td>
					<td>
						<xsl:variable name="anchor" select="translate(substring-before(@name,'.java'),'/','.')" />
						<a href="#{$anchor}">
							<xsl:value-of select="@name"/>
						</a>
					</td>
				</tr>
			</xsl:for-each>
		</table>
	</xsl:template>
	<!-- Print the list of errors for a single file, sorted by location. -->
	<xsl:template match="file">
		<xsl:variable name="anchor" select="translate(substring-before(@name,'.java'),'/','.')" />
		<a name="{$anchor}"/>
		
		<h3>
			<xsl:value-of select="@name"/>
			<span style="float:right"><xsl:value-of select="count(error)"/> Error<xsl:if test="count(error) != 1">s</xsl:if></span>
		</h3>
		<table>
			<tr>
				<th>Line &amp; Col</th>
				<th class="c">Found By</th>
				<th>Error Description</th>
			</tr>
			<xsl:for-each select="error">
				<tr>
					<xsl:call-template name="alternated-row"/>
					<td>
						<xsl:value-of select="@line"/>
						<xsl:if test="count(@column) > 0"> : <xsl:value-of select="@column"/></xsl:if>
					</td>
					<td class="c">
						<xsl:call-template name="checkstyle-link">
							<xsl:with-param name="class" select="@source"/>
						</xsl:call-template>
					</td>
					<td class="w">
						<xsl:value-of select="@message"/>
					</td>
				</tr>
			</xsl:for-each>
		</table>
	</xsl:template>
	
	<!-- From the qualified path name, create a link to the source CheckStyle check. -->
	<xsl:template name="checkstyle-link">
	  <xsl:param name="class"/>
	  <xsl:variable name="short" select="substring-after($class, 'com.puppycrawl.tools.')"/>
	  <xsl:variable name="source" select="substring-after($short, 'checkstyle.checks.')"/>
	  <xsl:variable name="group" select="substring-before($source, '.')" />
	  <xsl:variable name="check" select="substring-after(substring-before($source, 'Check'), '.')" />
	  <xsl:variable name="href" select="concat('http://checkstyle.sourceforge.net/config_', $group, '.html#', $check)"/>
	  <a href="{$href}" title="{$short}">Module</a>
	</xsl:template>
	<!-- Alternate between shades of gray for successive table rows. -->
	<xsl:template name="alternated-row">
		<xsl:attribute name="class">
			<xsl:if test="position() mod 2 = 1">a</xsl:if>
			<xsl:if test="position() mod 2 = 0">b</xsl:if>
		</xsl:attribute>
	</xsl:template>
</xsl:stylesheet>
